package com.fjec.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.user.entity.SysUser;
import com.fjec.user.service.ISysUserService;
import com.fjec.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 002.用户表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@Slf4j
@RestController
@RequestMapping("/")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Resource
    private IUserService userService;

    @PostMapping("/user/save")
    public JsonResult saveUser(SysUser sysUser) {
        return sysUserService.saveUser(sysUser);
    }

    @PostMapping("/user/update")
    public JsonResult updateUser(@RequestBody SysUser sysUser) {
        return sysUserService.updateUser(sysUser);
    }

    @GetMapping("/user/getById")
    public JsonResult getUser(@RequestParam String userName) {
        return JsonResult.success()
                .data(sysUserService.getUserByName(userName))
                .build();
    }

    @GetMapping("/user/page")
    public JsonResult getUsersPage(Page page) {
        return sysUserService.getUsersPage(page);
    }

    /**
     * 更新用户电话号码
     */
    @PostMapping("/user/phone/update")
    public int updateUserPhone(Long userId, String phone) {
        int result;
        try {
            result = sysUserService.updateUserPhone(userId, phone);
        } catch (DuplicateKeyException e) {
            result = 3014;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = -1;
        }
        return result;
    }

    /**
     * 为创建商户更新用户信息
     *
     * @param dataMap
     * @return 1=更新成功； 3014=电话号码重复； -1=更新失败
     */
    @PostMapping("/update/for/create/shop")
    public int updateUserInfoForCreateShop(@RequestBody Map<String, Object> dataMap) {
        int result = 1;
        try {
            userService.updateUserInfoForCreateShop(dataMap);
        } catch (DuplicateKeyException e) {
            result = 3014;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = -1;
        }
        return result;
    }

    /**
     * 电话号码是否存在
     *
     * @param phone
     * @return
     */
    @GetMapping("/phone/exists")
    public boolean existsPhone(String phone) {
        return userService.existsPhone(phone);
    }

    @GetMapping("/user/getOpenIdByUserId")
    public Map<String, Object> getOpenIdByUserId(Long userId) {
        String openId = sysUserService.getOpenIdByUserId(userId);
        Map<String, Object> result = new HashMap<>();
        result.put("openId", openId);
        return result;
    }
}
